package br.com.naybee.client.views.add.menu.automacao;

import br.com.naybee.client.NaybeeService;
import br.com.naybee.client.views.add.repartimento.RepartimentoFuncaoView;
import br.com.naybee.client.views.base.Principal;
import br.com.naybee.framework.client.componente.gwt.event.CommEvent;
import br.com.naybee.framework.client.componente.gwt.event.CommHandler;
import br.com.naybee.framework.client.componente.gwt.web.Div;
import br.com.naybee.framework.client.models.Model;
import br.com.naybee.framework.client.utils.NAYException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class MenuItemView extends Composite {

	private static MenuItemUiBinder uiBinder = GWT.create(MenuItemUiBinder.class);

	interface MenuItemUiBinder extends UiBinder<Widget, MenuItemView> {
	}

	@UiField
	protected Div icone;
	@UiField
	protected Div descricao;

	private Model param;

	public MenuItemView(Model model) {
		initWidget(uiBinder.createAndBindUi(this));
		param = model;
		init();
	}

	public MenuItemView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	private void init() {
		NaybeeService naybeeService = new NaybeeService("iconeFs", "consultarIcones");
		naybeeService.post(param, new CommHandler() {
			public void onSuccess(CommEvent event) {
				carregarMenuItem(event.getModel());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private void carregarMenuItem(Model model) {
		descricao.setText(model.getStr("descricaoRepartimento"));
		icone.setClassName(icone.getClassName() + " " + model.getStr("icone"));
	}

	@UiHandler("btnMenuItem")
	public void onClickBtnMenuItem(ClickEvent event) {
		Principal.get().add(new RepartimentoFuncaoView(param));
	}

	public Model getParam() {
		return param;
	}

}
